package edu.mis.model.goods;

import com.jfinal.core.Controller;

import edu.mis.model.Category;
import edu.mis.model.Goods;


public class GoodsController extends Controller {
	
	static GoodsService service = new GoodsService();
	
	public void index() {
		render("goods.html");
	}
	
	public void list(){
		setAttr("data", service.findAllGoodsWithCategory());
		renderJson();  
	}

	public void add() {
		    //render("add.html");  
	}

	public void register(){
		Goods goods  =  getModel(Goods.class);  
		if(service.register(goods)){
			
			setAttr("result", true); 
			setAttr("msg", "添加商品成功!"); 
		}else{
			setAttr("result",false);
			setAttr("msg", "添加失败，商品名重复!"); 
		}
		renderJson(); 
	}
	
	public void edit() {
		setAttr("goods", service.findGoodsById(getParaToInt("id"))); 
	}

	public void updatePrice(){
		int id=this.getParaToInt("id");  
		int newPrice = this.getParaToInt("price");
		if(service.updatePrice(id, newPrice)){
			setAttr("result", true);
			setAttr("msg", "修改价格成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	
	public void auth() {
		setAttr("goods", service.findGoodsById(getParaToInt("id")));  
		setAttr("category",Category.dao.findAll());
	}
	

	public void authorize(){
		int id=this.getParaToInt("id");   
		int categoryId = this.getParaToInt("categoryId");
		if(service.goodsAuthorize(id, categoryId)){
			setAttr("result", true);
			setAttr("msg", "商品类别修改成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	
	

}


